package com.mk.es.service.repertory;

import com.mk.es.service.entity.AbstractEsEntry;
import com.mk.es.service.transport.EsTransportOpt;
import com.mk.es.service.transport.entity.EsQueryBuilder;
import com.mk.es.service.transport.entity.EsQueryResult;
import com.mk.es.service.transport.handler.EsSearchResponseHandler;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

/**
 * ES 的仓库类
 */
public abstract class AbstractEsRepertoryAdapter<T extends AbstractEsEntry> implements InterfaceEsRepertory<T>  {

    @Resource(name="esTransportOpt")
    protected EsTransportOpt esTransportOpt;

    /** 高亮标签 开始标签*/
    public final static String preTags = "<em>";
    /** 高亮标签 结束标签*/
    public final static String postTags = "</em>";

    private String indexName; //索引名称
    private String typeName; //索引类型

    public AbstractEsRepertoryAdapter(String indexName, String typeName) {
        this.indexName = indexName;
        this.typeName = typeName;
    }

    /**
     * 添加索引类型Mapping
     * @param mapping
     * @return
     */
    @Override
    public boolean putMapping(String mapping){
        return esTransportOpt.putMapping(indexName, typeName, mapping);
    }

    /**
     * 添加文档
     * @param object
     * @return
     */
    @Override
    public void addOrUpdateDocument(T object){
        esTransportOpt.addDocument(indexName, typeName, object.getId(), object);
    }

    /**
     * 更新文档
     * @param object
     * @return
     */
    @Override
    public void updateDocument(T object){
        esTransportOpt.updateDocument(indexName, typeName, object.getId(), object);
    }

    /**
     * 更新部分文档
     * @param map
     * @return
     */
    @Override
    public void updateDocumentPartInfo(String id, Map<String, Object> map){
        esTransportOpt.updatePartDocument(indexName, typeName, id, map);
    }

    /**
     * 删除文档
     * @param id
     * @return
     */
    @Override
    public boolean delDocument(String id) {
        return esTransportOpt.delDocument(indexName, typeName, id);
    }

    /**
     * 获取文档信息
     * @param id
     * @return
     */
    @Override
    public T getDocument(String id) {
        //获取 T 对应的Class
        Class <T> clazz = (Class <T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return esTransportOpt.getDocument(indexName, typeName, id, clazz);
    }

    /**
     * 查询对应的类
     *
     * queryJsonStr对应如下，可以按需求更改
     *
     * @param queryJsonStr
     * @param params
     * @param <T>
     * @return
     */
    @Override
    public <T> EsQueryResult<T> querySearch(String queryJsonStr, Map<String, Object> params) {
        //获取 T 对应的Class
        Class <T> clazz = (Class <T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return esTransportOpt.querySearch(indexName, typeName, queryJsonStr, params, clazz);
    }

    /**
     * ES 查询
     * @param queryJsonStr 查询语句
     * @param params 查询参数
     * @param handlerAdapter 查询处理类
     * @param <T>
     * @return
     */
    @Override
    public <T> EsQueryResult<T> querySearch(String queryJsonStr, Map<String, Object> params, EsSearchResponseHandler<T> handlerAdapter) {
        //获取 T 对应的Class
        Class <T> clazz = (Class <T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return esTransportOpt.querySearch(indexName, typeName, queryJsonStr, params, clazz, handlerAdapter);
    }

    /**
     * ES 查询
     * @param esQueryBuilder 查询
     * @param <T>
     * @return
     */
    @Override
    public <T> EsQueryResult<T> querySearch(EsQueryBuilder esQueryBuilder) {
        //获取 T 对应的Class
        Class <T> clazz = (Class <T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return esTransportOpt.querySearch(indexName, typeName, esQueryBuilder, clazz);
    }

    /**
     * ES 查询
     * @param esQueryBuilder 查询
     * @param <T>
     * @return
     */
    @Override
    public <T> EsQueryResult<T> querySearch(EsQueryBuilder esQueryBuilder, EsSearchResponseHandler<T> handlerAdapter) {
        //获取 T 对应的Class
        Class <T> clazz = (Class <T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return esTransportOpt.querySearch(indexName, typeName, esQueryBuilder, clazz, handlerAdapter);
    }

}
